package edu.hunan.cxb18.controller;

import edu.hunan.cxb18.car.Car;
import edu.hunan.cxb18.error.impl.UserException;
import edu.hunan.cxb18.pojo.Address;
import edu.hunan.cxb18.pojo.Order;
import edu.hunan.cxb18.pojo.User;
import edu.hunan.cxb18.result.CommonTypeResult;
import edu.hunan.cxb18.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author AuthorName
 * @create 2020-06-11 0:19
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @RequestMapping("/save")
    public CommonTypeResult save(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user=(User) session.getAttribute("user");
        Car car=(Car)session.getAttribute("car");
        try {
            String oid = orderService.createOrder(user,car);
            return CommonTypeResult.creat("oid="+oid,"success");
        } catch (UserException e) {
            return CommonTypeResult.creat(e.getErrMsg(),"false");
        }
    }
    @RequestMapping("/load")
    public CommonTypeResult load(@RequestParam(name = "oid") String oid){
        try {
            Order order = orderService.queryByOidWithItems(oid);
            return CommonTypeResult.creat(order,"success");
        } catch (UserException e) {
            return CommonTypeResult.creat(e.getErrMsg(),"false");
        }
    }
    @RequestMapping("/address")
    public CommonTypeResult address(@RequestParam(name = "address") String address1,@RequestParam(name = "receciver") String receciver,
                                    @RequestParam(name = "tel") String tel,@RequestParam(name = "uid") String uid){
        Address address = new Address();
        address.setAddress(address1);
        address.setRececiver(receciver);
        address.setTel(tel);
        address.setUid(1);
        try {
            orderService.payOrder(address);
            return CommonTypeResult.creat(address,"success");
        } catch (UserException e) {
            return CommonTypeResult.creat(e.getErrMsg(),"false");
        }
    }
}
